/**
 * @param {Int} opts.rows
 * @param {Function} opts.rowsError
 * @param {Function} opts.error
 * @param {Function} opts.end
 */
$.fn.formatDomain = function(opts){
	var defaults = {
		rows:100,
		rowsError:function(rows){
			alert('输入不能超过' + rows + '行');
		},
		error:function(el){
			alert(el + '不是一级域名');
		},
		end:function(status){},
	}
	var setter = $.extend({},defaults,opts);

	function formatDomainObj(){
		this.list=[];
		this.rows = setter.rows;
		this.status = true;
	};

	/**
	 * @param {string} value
	 */
	formatDomainObj.prototype.strToArray = function(value){
		var reg = /\n/g;
		var list = value.split(reg);
		return list;
	}
	/**
	 * @param {Array} list
	 */
	formatDomainObj.prototype.filterSpace = function(list){
		Array.prototype.indexOf = function(val){
			for(var i=this.length;i--;){
				if(this[i]==val){
					return i;
				}
			}
			return -1;
		}
		for(var i=list.length;i--;){
			var str = this.removeSpace(list[i]);
			if(str ==''){
				list.splice(list.indexOf(list[i]),1);
			}
		}
	}
	/**
	 * @param {String} value
	 */
	formatDomainObj.prototype.isDomain = function(value){
		var reg = /^[a-zA-Z0-9\!@#\$%\^&\*\(\)_-]+\.(com|cn|xin|ltd|vip|xyz|club|wang|store|pub|cc|site|top|net|ren|lol|bid|mom|online|tech|biz|red|website|space|link|news|date|loan|mobi|press|video|market|live|studio|help|info|win|click|pics|photo|trade|science|party|rocks|band|gift|wiki|design|software|social|lawyer|engineer|org|com\.cn|net\.cn|gov\.cn|name|tv|me|asia|co|\u6211\u7231\u4f60|work|\u4e2d\u56fd|\u516c\u53f8|\u7f51\u7edc|\u96c6\u56e2|game)$/i;
		try{
			return reg.test(value);
		}catch(err){
			throw err;
		}
	}
	formatDomainObj.prototype.isRegex = function(reg,value){
		patt = eval(reg);
		try{
			return patt.test(value);
		}catch(err){
			throw err;
		}
	}
	function limitRows(){
		if(format.list.length>format.rows){
			setter.rowsError(setter.rows);
//			format.list.length=setter.rows;
			format.status = false;
		}
	}
	function validate(me,that){
		var msg = "";
		var value = me.val();
		if(value==""){
			me.attr("type","false");
		}else{
			me.attr("type","true");
		}
		if(me.attr("regex")){
			var patt = me.attr("regex");
		}else{
			var patt = /^[a-zA-Z0-9\!@#\$%\^&\*\(\)_-]+\.(com|cn|xin|ltd|vip|xyz|club|wang|store|pub|cc|site|top|net|ren|lol|bid|mom|online|tech|biz|red|website|space|link|news|date|loan|mobi|press|video|market|live|studio|help|info|win|click|pics|photo|trade|science|party|rocks|band|gift|wiki|design|software|social|lawyer|engineer|org|com\.cn|net\.cn|gov\.cn|name|tv|me|asia|co|\u6211\u7231\u4f60|work|\u4e2d\u56fd|\u516c\u53f8|\u7f51\u7edc|\u96c6\u56e2|game)$/i;
		}
		format.list = format.strToArray(value);
		for(var i=format.list.length;i--;){
			var val = $.trim(format.list[i]);
			if(val==""){
				continue;
			}
			if(!format.isRegex(patt,val)){
				msg+=val +",";
			}
		}
		if(msg.length>0){
			format.status = false;
		}else{
			format.status = true;
		}
		if(!format.status){
			setter.error(msg);
			me.attr("type","false");
		}else{
			setter.end(format.status);
			me.attr("type","true");
		}
	};

	var format = new formatDomainObj();
	this.each(function(){
		var me = $(this);
		me.attr("type","false");
		me.on('input blur',function(){
			var that = this;
			validate(me);
		});
		$("*").not(me).on("click",function(){
			validate(me);
		})
	});
}

